package com.bawei.day16_aidl_client;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import com.bawei.day16_aidl_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    IMyAidlInterface iMyAidlInterface;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidlInterface =  IMyAidlInterface.Stub.asInterface(service);
            try {
                int add = iMyAidlInterface.add(3, 4);
                Toast.makeText(MainActivity.this, ""+add, Toast.LENGTH_SHORT).show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Toast.makeText(MainActivity.this, "111", Toast.LENGTH_SHORT).show();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent();
        intent.setAction("com.bawei.add");//设置action
        intent.setPackage("com.bawei.day16_aidl_server");//设置服务端应用进程包名
        bindService(intent,connection, Service.BIND_AUTO_CREATE);



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}
